package com.demo.service;

import com.demo.bean.Result;
import com.demo.bean.User;
import com.demo.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: maxwellens
 */
@Service
public class UserService
{
    @Autowired
    private UserDao userDao;

    public List<User> findUsers(Map<String, Object> map)
    {
        return userDao.findUsers(map);
    }

    public Integer findUsersCount(Map<String, Object> map)
    {
        return userDao.findUsersCount(map);
    }

    public Result findUsersResult(Map<String, Object> map)
    {
        Integer page = (Integer) map.get("page");
        Integer limit = (Integer) map.get("limit");
        if (page != null && limit != null)
        {
            map.put("start", (page - 1) * limit);
            map.put("length", limit);
        }
        int count = userDao.findUsersCount(map);
        List<User> data = userDao.findUsers(map);
        return new Result(data, count);
    }

    public User findUserById(Integer id)
    {
        return userDao.findUserById(id);
    }

    public void saveUser(User user)
    {
        if (user.getId() != null && user.getId() != 0)
        {
            userDao.updateUser(user);
        } else
        {
            userDao.insertUser(user);
        }
    }

    public void deleteUserById(Integer id)
    {
        userDao.deleteUserById(id);
    }

    public void deleteUsers(int[] ids)
    {
        userDao.deleteUsers(ids);
    }

}
